import {RECIEVE_ADDRESS,
        RECIEVE_SHOPS,
        RECIEVE_CATEGORYS,
        RECEIVE_USER_INFO,
        RECEIVE_RATINGS,
        RECIEVE_GOODS,
        RECIEVE_INFO,
        INCREMENT_FOOD_COUNT,
        DECREMENT_FOOD_COUNT,
        CLEAR_CART,
        RECEIVE_SEARCH_SHOPS,
        RESET_USER
}

from "@/store/mutation-types";
import Vue from 'vue'
export default {
  [RECIEVE_ADDRESS] (state,{address}){
    state.address = address
  },
  [RECIEVE_CATEGORYS] (state,{categorys}){
    state.categorys = categorys
  },
  [RECIEVE_SHOPS] (state,{shops}){
    state.shops = shops
  },
  [RECEIVE_USER_INFO] (state,{userInfo}){
    state.userInfo = userInfo
  },
  [RECIEVE_GOODS] (state,{goods}){
    state.goods = goods
  },
  [RECEIVE_RATINGS] (state,{ratings}){
    state.ratings = ratings
  },
  [RECIEVE_INFO] (state,{info}){
    state.info = info
  },
  [INCREMENT_FOOD_COUNT] (state,{food}) {
    if(!food.count) {
      Vue.set(food,'count',1)
      state.cartFoods.push(food)
    }else {
      food.count++
    }
  },
  [DECREMENT_FOOD_COUNT] (state,{food}) {
    if(food.count) {
      food.count--
      if(food.count === 0) {
        state.cartFoods.slice(state.cartFoods.indexOf(food),1)
      }
    }
  },
  [CLEAR_CART] (state) {
    //清除food中的count
    state.cartFoods.forEach(food => food.count = 0)
    //移除购物车的所有购物项
    state.cartFoods = []
  },
  [RECEIVE_SEARCH_SHOPS] (state,{searchShops}) {
    state.searchShops = searchShops
  }
}